package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsShipmentOrder;
import com.jy.wms.pojo.WmsShipmentOrderDetailed;
import com.jy.wms.service.OrderInterfaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: UserController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author lh
 * @Date 2018/5/31 0031
 */
@RestController
@RequestMapping("/orderInterfaceController")
@Api(value = "/wmsshipmentorder", description = "关于对接接口操作lh")
public class OrderInterfaceController extends BaseController<OrderInterfaceService, WmsShipmentOrderDetailed> {

    /**
     * 提取 查对方接口的参数以及路径
     * @param entity
     * @param posturl
     * @return
     * @throws Exception
     */
    private JSONObject getJsonObject(@RequestBody WmsShipmentOrder entity, String posturl) throws Exception {
        JSONObject json=new JSONObject();
        json.put("works","S.01");
        json.put("sono",entity.getSono());
        json.put("sotype",entity.getSotype());
        if( entity.getSotype().equals("140")){
            json.put("whouse","SA01");
        }else if(entity.getSotype().equals("104")){
            json.put("whouse","SB01");
        }
        json= InterfaceCall.allocation(json,posturl);
        return json;
    }

    @ApiOperation(value = "获取返回erp订单信息", response = JSONObject.class, notes = "{\"sono\":\"订单号\",\"sotype\":\"订单类型\"}")
    @PostMapping(value = "/getDBIssueOrder")
    public @ResponseBody
    JSONObject getDBIssueOrder(@RequestBody WmsShipmentOrder entity, HttpSession session) throws Exception {
        List list = new ArrayList();
        JSONObject jsonObject = new JSONObject();
        //用户信息
        WmsShipmentOrder wmsShipmentOrder = null;
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库id
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = null;
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                entity.setSshzid(hzid);
            }
            String posturl="";
            if(entity.getSotype().equals("140")){
                posturl = "/easApi/getDBIssueOrder";
            }else if(entity.getSotype().equals("104")){
                posturl="/easApi/getMaterialReqOrder";
            }
            entity.setCkid(ckid);
            JSONObject json = getJsonObject(entity, posturl);
            wmsShipmentOrder = baseService.getDBIssueOrder(entity, userinfo.getUser().getName(),json);
            list.add(wmsShipmentOrder);
            jsonObject = SetStatus.setSearchType(JSONArray.fromObject(list).toString(), list.size());
        } catch (ServiceException e) {
            jsonObject = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "erp订单信息查询失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "erp订单信息查询失败", "erp订单信息查询失败", "");
        }
        return jsonObject;
    }


    @PostMapping(value = "save")
    @ApiOperation(value = "保存erp订单信息", response = JSONObject.class, notes = "保存erp订单信息")
    public @ResponseBody
    JSONObject saveErpOrder(@RequestBody WmsShipmentOrder entity, HttpSession session) {
        JSONObject jsonObject = getJSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库id
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = null;
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
            }
            entity.setCjr(userinfo.getUser().getName());
            entity.setCkid(ckid);
            entity.setSshzid(hzid);
            WmsShipmentOrder count=baseService.getCountByfybh(entity.getSono());
            if(count==null){
                String posturl="";
                if(entity.getSotype().equals("140")){
                    posturl = "/easApi/getDBIssueOrder";
                }else if(entity.getSotype().equals("104")){
                    posturl="/easApi/getMaterialReqOrder";
                }
                JSONObject json = getJsonObject(entity,posturl);
                if(json!=null){
                    baseService.InsertDBPoOrder(json,entity);
                }
                jsonObject.put("backcode",-1);
                jsonObject.put("backmsg",entity.getSono()+"发运单号和订单类型不匹配,不可添加");

            }else {
                if(count.getLxdm().equals(entity.getSotype())){
                    jsonObject.put("backcode",-1);
                    jsonObject.put("backmsg",entity.getSono()+"发运单号已存在,不可重复添加");
                }else{
                    jsonObject.put("backcode",-1);
                    jsonObject.put("backmsg",entity.getSono()+"发运单号和订单类型在数据库内不匹配");
                }
                /*jsonObject.put("backcode",-1);
                jsonObject.put("backmsg",entity.getSono()+"发运单号已存在,不可重复添加");*/
                // jsonObject= SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.warning.toString(), "ERP订单增加失败", "发运订单已存在", "");
            }
            // return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "ERP订单增加成功", "", JSONObject.fromObject(entity).toString());
        } catch (SucessException e) {
            e.printStackTrace();
            jsonObject= SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "ERP订单增加成功", e.getMessage(), JSONObject.fromObject(entity).toString());
        } catch (ServiceException e) {
            e.printStackTrace();
            jsonObject= SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "ERP订单增加失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject= SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "ERP订单增加失败", e.getMessage(), "");
        }
        return  jsonObject;
    }
    ///////////////////////********新增接口*****出库被动******************///////////////////
    /**
     * 1.出库订单状态的查询,
     * @param requestBody
     * @return
     */
    //@RequestMapping(value="/getIssueOrderStatus",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
    @PostMapping(value = "/getIssueOrderStatus")
    @ApiOperation(value = "出库订单状态的查询",response = JSONObject.class, notes = "出库订单状态的查询")
    public @ResponseBody JSONObject getIssueOrderStatus(@RequestBody String requestBody) {
        JSONObject jsonObject = JSONObject.fromObject(requestBody);
        JSONObject json=new JSONObject();
        WmsShipmentOrder wmsShipmentOrder = new WmsShipmentOrder();
        try{
            wmsShipmentOrder = baseService.GetIssueOrderStatus(String.valueOf(jsonObject.get("sono")),String.valueOf(jsonObject.get("works")));
            if(wmsShipmentOrder==null){
                json.put("backcode",-1);
                json.put("sostatus","4");
                json.put("backmsg","出库单不存在");
            }else{
                json = baseService.GetIssueOrderStatusOrder(wmsShipmentOrder);
                System.out.println(json.toString());
                json.put("backcode",0);
                json.put("backmsg","查询出库订单状态成功");
            }
        }catch (Exception e) {
            json.put("backcode",-1);
            json.put("backmsg","查询出库订单状态失败");
        }
        return json;
    }

    /**
     * 销售出库单的修改
     * @param requestBody
     * @return
     */
    @PostMapping(value = "/getChangeIssueOrder")
    @ApiOperation(value = "出库订单的修改 修改发运订单",response = JSONObject.class, notes = "出库订单的修改 修改发运订单")
    public @ResponseBody JSONObject getChangeIssueOrder(@RequestBody String requestBody) {
        /*requestBody=requestBody.replace("\"{","{");
        requestBody=requestBody.replace("}\"","}");*/
        //JSONObject jsonObject = JSONObject.fromObject(requestBody.replaceAll("\\\\",""));
        JSONObject jsonObject = JSONObject.fromObject(requestBody);
        JSONObject json=new  JSONObject();
        try{
            System.out.println(jsonObject.toString());
            baseService.GetChangeIssueOrder(jsonObject);
        }  catch (SucessException e) {
            json.put("backcode",0);
            json.put("backmsg","出库订单的修改成功");
        }catch (RuntimeException e) {
            json.put("backcode",-1);
            json.put("backmsg","出库订单不可修改");
        }catch (Exception e) {
            json.put("backcode",-1);
            json.put("backmsg","出库订单的修改失败");
        }
        return json;
    }

    /**
     * 销售出库单的取消
     * @param requestBody
     * @return
     */
    @PostMapping(value = "/cancelIssueorder_BD")
    @ApiOperation(value = "销售出库单的取消",response = JSONObject.class, notes = "销售出库单的取消")
    public @ResponseBody JSONObject cancelIssueorder_BD(@RequestBody String requestBody) {
        JSONObject jsonObject = JSONObject.fromObject(requestBody);
        JSONObject json=new  JSONObject();
        try{
            baseService.CancelIssueorder_BD(jsonObject);
        }  catch (SucessException e) {
            json.put("backcode",0);
            json.put("backmsg","销售出库单取消成功");
        }catch (RuntimeException e) {
            json.put("backcode",-1);
            json.put("backmsg","销售出库单不可取消");
        }catch (Exception e) {
            json.put("backcode",-1);
            json.put("backmsg","销售出库单取消失败");
        }
        return json;
    }
}
